<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class personal_health_info_analyse extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('customer_model');
		$this->load->model('personal_health_info_model');
	}
	
//	public function open_analyse_result_page()
//	{
//		$this->load->view("functionEntry/personalHealthInfoAnalyse/health_info_analyse_result");
//	}
	
	public function update_symptom()
	{
		$results = $this->input->post('field');
		$accepted = $this->input->post('checked');
		
		$curArr = explode(',', $results);
		
		$lastArr = $this->personal_health_info_model->get_symptoms(get_uid());

		if($accepted == "true")
		{
			if(count($lastArr) == 0)
			{
				foreach ($curArr as $cur)
				{
					$this->personal_health_info_model->add_symptom(get_uid(), get_symptom_id_by($cur), 0);
				}
					
				echo json_encode($curArr[0]);
				//redirect(site_url("personal_health_info_analyse"));
				return;
			}
			
			foreach ($curArr as $cur)
			{
				$found = false;
				foreach($lastArr as $last)
				{
					if($cur == $last)
					{
						$found = true;
						break;
					}
				}
					
				if($found == false)
				{
					$this->personal_health_info_model->add_symptom(get_uid(), get_symptom_id_by($cur), 0);
					echo json_encode($cur);
					return;
				}
			}
		}
		else 
		{
			if(count($lastArr) == 0)
			{
				return;
			}
							
			$found = false;
			foreach ($curArr as $cur)
			{
				foreach($lastArr as $last)
				{
					if($cur == $last)
					{
						$found = true;
						break;
					}
				}
					
				if($found == true)
				{
					$this->personal_health_info_model->delete_symptom(get_uid(), get_symptom_id_by($cur));
					echo json_encode($cur);
					return;
				}
			
				$found = false;
			}
		}
	}
	
	public function update_symptom_share()
	{
		$results = $this->input->post('field');
		$shared = $this->input->post('checked');
		$curArr = explode(',', $results);		
		$this->personal_health_info_model->share_symptom(get_uid(), get_symptom_id_by($curArr[0]), $shared);
	}
	
/*	private function delete_outdated_symptom()
	{
		$lastSymptoms = $this->personal_health_info_model->get_symptoms(get_uid());
		$curSymptoms = $this->personal_health_info_model->get_analyse_result(get_uid());
		
		$diff = array_diff($lastSymptoms, $curSymptoms);
		
		$escape = array("困", "乏", "晕", "闷");
		foreach($diff as $symptom_name){
			if(in_array($symptom_name, $escape))
				continue;

			$symptomType = get_symptom_id_by($symptom_name);
			
			$this->personal_health_info_model->delete_symptom(get_uid(), $symptomType);
			
		}
	}
*/
	public function index()
	{		
		$data = array();
		$data['title'] = '个人健康分析';
		$data['lastLoginTime'] = $this->customer_model->get_last_login(get_uid());
		$data['totalScore'] = '80';
		
		$lastArr = $this->personal_health_info_model->get_symptoms(get_uid());
		$curArr = $this->personal_health_info_model->get_analyse_result(get_uid());
		
		$data['symptoms'] = $lastArr;
		$data['analyseResult'] = $curArr;
		
		$flags = array();
		$shareds = array();
		$cur_shareds = $this->personal_health_info_model->get_symptoms_share(get_uid());
		
		$found = false;
		foreach ($curArr as $cur)
		{
			foreach($lastArr as $last)
			{
				if($cur == $last)
				{
					$found = true;
				}
			}
				
			if($found)
			{
				array_push($flags, true);
				array_push($shareds, $cur_shareds[$cur]);
			}
			else
			{
				array_push($flags, false);
				array_push($shareds, false);
			}
			
			$found = false;
		}
		
		$data['existSymptoms'] = $flags;
		$data['sharedSymptoms'] = $shareds;
		
		$this->load->view("functionEntry/personalHealthInfoAnalyse/personalHealthInfoAnalyse", $data);
	}
	
}
?>